package com.gjzhao.fmmall.controller;

import com.gjzhao.fmmall.entity.Orders;
import com.gjzhao.fmmall.service.ApiOrderService;
import com.gjzhao.fmmall.vo.RespResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: Api 订单处理接口
 * @Author: gjzhao
 * @Date Created in 2022-02-08 9:02 下午
 */
@RestController
@RequestMapping("/ordersHystrix")
@CrossOrigin
@Api(tags = "订单管理", value = "订单处理接口")
public class ApiOrderController extends BaseController {

    @Autowired
    private ApiOrderService apiOrderService;


    /**
     * 接口作用：处理订单
     * 业务流程分析：
     * - 1.用户发送输入需要添加的订单信息
     * - 2.调用api-order-add-feign服务的该接口
     *  - 2.1 在该服务的业务处理层，使用自定义feign客户端发送rest请求(post)[feign组件是对ribbon与loadbalancer的封装]
     *      - 2.1.1 根据url中配置的服务名字去eureka服务注册与发现中心获取到该服务对应的地址列表
     *      - 2.1.2 根据负载均衡算法(轮询)，选择其中一个地址
     *  - 2.2 根据选择的地址，发送服务调用请求
     *  - 2.3 一层一层返回响应
     *
     * @param order
     * @param cartIds
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加订单")
    @ApiImplicitParam(dataType = "string", name = "cartIds", value = "购物车ids", required = true)
    public RespResultVO add(@RequestBody Orders order,
                            String cartIds) {

        RespResultVO resultVO = null;
        try {
            resultVO = apiOrderService.addOrder(cartIds, order);
        } catch (Exception e) {
            logger.info("-----提交订单失败:[" + e.getMessage() + "]-----");
            resultVO = RespResultVO.failed("发生异常，提交订单失败");
        }
        return resultVO;
    }

    @GetMapping("/getOrder")
    @ApiOperation(value = "查询订单")
    @ApiImplicitParam(dataType = "string", name = "orderId", value = "订单id", required = true)
    public RespResultVO getOrder(String orderId) {

        RespResultVO resultVO = null;
        try {
            resultVO = apiOrderService.getOrder(orderId);
        } catch (Exception e) {
            logger.info("-----查询订单失败:[" + e.getMessage() + "]-----");
            resultVO = RespResultVO.failed("发生异常，查询订单失败");
        }
        return resultVO;
    }

    @GetMapping("/testGateway")
    @ApiOperation(value = "查询订单")
    @ApiImplicitParam(dataType = "string", name = "orderId", value = "订单id", required = true)
    public RespResultVO testGateway(@RequestHeader("flag") String flag, String name,String orderId) {

        logger.info("-----测试请求头中flag字段内容:{}-----",flag);
        logger.info("-----测试请求参数中name字段内容:{}-----",name);

        RespResultVO resultVO = null;
        try {
            resultVO = apiOrderService.getOrder(orderId);
        } catch (Exception e) {
            logger.info("-----查询订单失败:[" + e.getMessage() + "]-----");
            resultVO = RespResultVO.failed("发生异常，查询订单失败");
        }
        return resultVO;
    }
}
